/*
 * PanelMap.java
 *
 * Created on 23. duben 2008, 21:16
 */
package net.slavics.maps;

import net.slavics.maps.GridMap;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import net.slavics.maps.ifc.IMapa;
import net.slavics.maps.ifc.MapComponent;
import org.apache.log4j.Logger;

/**
 *
 * @author  Petr Slavíček, Dis. <pslavicek@seznam.cz>
 */
public class PanelMap extends JPanel {

    private static Logger _log = Logger.getLogger(PanelMap.class);
    static final long serialVersionUID = 451321314L;
    /** Creates new form PanelMap */
    private Point screenZero;
    private Point dragging = null;
    private GridMap gMap;
    protected MapComponent owner;

    public PanelMap(MapComponent owner, IMapa iMapa, Point size) {
        _log.info("Založení Panelu " + size);
        this.owner = owner;
        initComponents();
        screenZero=iMapa.getCenterXY();
        gMap = new GridMap(owner,iMapa, screenZero);
        setLayout(new BorderLayout());
        add(gMap, BorderLayout.CENTER);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setFont(new java.awt.Font("Verdana", 0, 12));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        _log.debug("MousePressed=" + evt.getPoint() + " " + evt.getButton());
        if (dragging == null && evt.getButton() == MouseEvent.BUTTON1) {
            dragging = evt.getPoint();
            setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }
    }//GEN-LAST:event_formMousePressed

    private void formMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseDragged
        if (dragging != null) {
            Point der = new Point(evt.getX() - dragging.x, evt.getY() - dragging.y);
            screenZero.translate(-der.x, -der.y);
            gMap.shift(der);

            dragging = evt.getPoint();
        }
        formMouseMoved(evt);
    }//GEN-LAST:event_formMouseDragged

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        _log.debug("MouseReleased=" + evt.getPoint() + " " + evt.getButton());
        if (dragging != null && evt.getButton() == MouseEvent.BUTTON1) {
            dragging = null;
            setCursor(Cursor.getDefaultCursor());
        }
    }//GEN-LAST:event_formMouseReleased

    private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
        owner.setLabelSouradnice(evt.getPoint(), screenZero);
    }//GEN-LAST:event_formMouseMoved
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
